package cn.stylefeng.guns.modular.web.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.core.ParamUtil;
import cn.stylefeng.guns.modular.web.entity.PlUser;
import cn.stylefeng.guns.modular.web.service.IPlUserService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sql.rowset.serial.SerialException;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述：用户表 controller
 * 作者：chenhuiyan
 * 时间：2020-04-13
 */
@Controller
@RequestMapping("/web/user")
public class PlUserController extends BaseController {

    private String PREFIX = "/modular/web/user/";

    @Autowired
    private IPlUserService userService;

    /**
     * 跳转到管理首页
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX + "user.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list() {
        Map<String, Object> params = ParamUtil.getParams();
        Page<Map<String, Object>> list = userService.list(params);
        return LayuiPageFactory.createPageInfo(list);
    }

    /**
     * 跳转添加/编辑页面
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(String userId, Model model){
        model.addAttribute("userId", userId);
        return PREFIX + "user_add.html";
    }

    /**
     * 添加/更新
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("/save")
    public ResponseData save(PlUser user){
        if(ToolUtil.isEmpty(user.getId())){
            user.preInsert();
            userService.save(user);
        }else{
            user.preUpdate();
            userService.updateById(user);
        }
        return SUCCESS_TIP;
    }

    /**
     * 获取详情
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInfoById/{userId}")
    public Object getInfoById(@PathVariable("userId") String userId){
        PlUser user = userService.getById(userId);
        return user;
    }

    /**
     * 删除
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public ResponseData delete(@RequestParam(name = "userId") String userId){
        if(ToolUtil.isEmpty(userId)){
            throw new ServiceException(BizExceptionEnum.PARAM_ERROR);
        }
        PlUser user = userService.getById(userId);
        user.preUpdate();
        user.setDelFlag(1);
        userService.updateById(user);
        return SUCCESS_TIP;
    }

    /**
     * 跳转修改金币页面
     * @return
     */
    @RequestMapping(value = "/updateGoldIndex")
    public String updateGoldIndex(String userId, Model model){

        //获取用户金币值
        PlUser user = userService.getById(userId);

        model.addAttribute("userId", userId);
        model.addAttribute("goldNum", user.getGold());

        return PREFIX + "update_gold.html";
    }

    /**
     * 修改用户金币
     * @param userId 用户编号
     * @param changeType 变动类型（0增加，1减少）
     * @param changeGold 变动数量
     * @param reson 修改原因
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateGold")
    public ResponseData updateGold(String userId, Integer changeType, Integer changeGold, String reson){
        if(ToolUtil.isOneEmpty(userId, changeType, changeGold, reson)){
            throw new ServiceException(BizExceptionEnum.PARAM_ERROR);
        }
        userService.updateGold(userId, changeType, changeGold, reson);
        return SUCCESS_TIP;
    }

}
